package com.zq.zhufengfm;

import android.content.Intent;
import android.content.IntentFilter;
import android.os.Parcelable;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.zq.zhufengfm.entity.Tracks;
import com.zq.zhufengfm.service.TrackPlayerService;

import org.w3c.dom.Text;

import java.util.ArrayList;

public class TrackDetailActivity extends AppCompatActivity {

    @ViewInject(R.id.track_detail_close)
    private ImageView trackClose;

    @ViewInject(R.id.track_playBtn)
    private ImageButton imgBtnPlay;

    @ViewInject(R.id.track_previousBtn)
    private ImageButton previous;

    @ViewInject(R.id.track_nextBtn)
    private ImageButton nextBtn;

    @ViewInject(R.id.track_item_title)
    private TextView title;

    //获取数据：
    private ArrayList<Tracks> tracksList;
    private  int  position;

    //创建用于管理播放组件的意图对象：
    private Intent playerIntent;//播放服务组件的意图对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_track_detail);
        //使用注解：
        ViewUtils.inject(this);
        //获取所有的信息：
        tracksList = getIntent().getParcelableArrayListExtra(Constants.SERVICE_PLAY_LIST);
        //获取被点击的位置  信息
        position = getIntent().getIntExtra(Constants.CLICK_TRACKS_POSITION,0);

        title.setText(tracksList.get(position).getTitle());//更新标题数据

        playerIntent = new Intent(this,TrackPlayerService.class);//开启服务组件

        //放入数据
        playerIntent.putParcelableArrayListExtra(Constants.SERVICE_PLAY_LIST, tracksList);
        playerIntent.putExtra(Constants.CLICK_TRACKS_POSITION, position);

        startService(playerIntent);

        initViewEvent();
    }

    //初始化视图中的 单击事件：
    private void initViewEvent() {
        //当单击向下的关闭图标时   关闭activity
        trackClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //当单击 上一曲时：播放上一个电台：
        previous.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //重新开启服务：
                if (position > 0) {
                    //
                    position = position--;//更新当前正在播放的位置

                    Log.i("info", position + "");

                    playerIntent = new Intent(getApplicationContext(),TrackPlayerService.class);//开启服务组件

                    playerIntent.putParcelableArrayListExtra(Constants.SERVICE_PLAY_LIST, tracksList);
                    playerIntent.putExtra(Constants.CLICK_TRACKS_POSITION, position);

                    title.setText(tracksList.get(position).getTitle());

                    Log.i("info", tracksList.get(position).getTitle());

                    startService(playerIntent);
                }else{
                    Toast.makeText(getApplicationContext(),"当前已经是第一首...",Toast.LENGTH_LONG).show();
                }
            }
        });

        //当单击  下一曲时  播放下一首曲目
        nextBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //重新开启服务：
                if (position != tracksList.size()-1) {
                    //
                    position = position++;//更新当前正在播放的位置

                    Log.i("info",position+"");

                    playerIntent = new Intent(getApplicationContext(),TrackPlayerService.class);//开启服务组件

                    playerIntent.putParcelableArrayListExtra(Constants.SERVICE_PLAY_LIST, tracksList);
                    playerIntent.putExtra(Constants.CLICK_TRACKS_POSITION, position);

                    //更新标题数据
                    title.setText(tracksList.get(position).getTitle());

                    Log.i("info",tracksList.get(position).getTitle());

                    startService(playerIntent);
                }else{
                    Toast.makeText(getApplicationContext(),"当前已经是第一首...",Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopService(playerIntent);
    }

}
